package com.molyfun.parents.activity.report.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.attachment.AttachmentViewer;
import com.molyfun.parents.rest.bean.CourseDetailResponse;
import com.molyfun.parents.rest.bean.ResultlistBean;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:12/13/16.
 * 完成度控件 p+ p- p
 *
 *
 */
public class ReportProcessView extends LinearLayout implements View.OnClickListener{

    protected LayoutInflater layoutInflater;

    private Context context;

    @InjectView(R.id.title_layout)
    LinearLayout mTitleLayout;
    @InjectView(R.id.report_process_title)
    TextView mReportProcessTitle;
    @InjectView(R.id.drawer_img)
    ImageView mDrawerImg;
    @InjectView(R.id.report_process_target)
    TextView mReportProcessTarget;
//    @InjectView(R.id.complete_checkbox_layout)
//    LinearLayout mCompleteCheckboxLayout;
    @InjectView(R.id.complete_str)
    TextView mCompleteTv;
    @InjectView(R.id.report_attachmentviewer_layout)
    AttachmentViewer mAttachmentViewer;
    @InjectView(R.id.attachment_null)
    TextView mAttachmentNull;
    @InjectView(R.id.preference)
    TextView mPreference;
    @InjectView(R.id.content_layout)
    LinearLayout mContentLayout;

    private int mItemPosition = -1;

    public ReportProcessView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public ReportProcessView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ReportProcessView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_report_process, this);
        ButterKnife.inject(this);
        mTitleLayout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_layout:
                if(mOnReportProcessItemClick != null){
                    mOnReportProcessItemClick.onItemClick(mItemPosition);
                }
                break;
        }
    }

    public static interface OnReportProcessItemClick{
        void onItemClick(int position);
    }

    private OnReportProcessItemClick mOnReportProcessItemClick;

    /**
     * init view
     *
     */
    public void initData(int position, CourseDetailResponse.CourseDetailBean courseDetailBean, boolean showContent, OnReportProcessItemClick onReportProcessItemClick) {
//        this.mResultlistBeanList = resultlistBeans;
        this.mItemPosition = position;
        mOnReportProcessItemClick = onReportProcessItemClick;
        if(courseDetailBean == null){
            return;
        }
        mReportProcessTitle.setText(courseDetailBean.getCoursename());
        if(TextUtils.isEmpty(courseDetailBean.getCourseperformance())){
            mPreference.setText("无");
        }else{
            mPreference.setText(courseDetailBean.getCourseperformance());
        }
        mReportProcessTarget.setText(courseDetailBean.getCoursetarget());
        if(courseDetailBean.getPlanmediaobjectlist() != null && courseDetailBean.getPlanmediaobjectlist().size() > 0){
            mAttachmentViewer.setVisibility(View.VISIBLE);
            mAttachmentViewer.initData(courseDetailBean.getPlanmediaobjectlist());
            mAttachmentNull.setVisibility(View.GONE);
        }else{
            mAttachmentViewer.setVisibility(View.GONE);
            mAttachmentNull.setVisibility(View.VISIBLE);
        }
        if(courseDetailBean.getResultList() != null && showContent){
            mContentLayout.setVisibility(View.VISIBLE);
            mDrawerImg.setImageResource(R.mipmap.ic_table_up_srrow);

            for(ResultlistBean resultlistBean : courseDetailBean.getResultList()){
                if(resultlistBean.isIsselected()){
                    if(resultlistBean.getTypename().equals("+")) {
                        mCompleteTv.setText("独立完成");
                    }else if(resultlistBean.getTypename().equals("p+")) {
                        mCompleteTv.setText("辅助完成");
                    }else if(resultlistBean.getTypename().equals("p-")) {
                        mCompleteTv.setText("不能完成");
                    }else {
                        mCompleteTv.setText("未完成");
                    }
                    break;
                }else{
                    mCompleteTv.setText("无");
                }
            }



//            mCompleteCheckboxLayout.removeAllViews();
//            for(ResultlistBean resultlistBean : courseDetailBean.getResultList()){
//                View completeCellView = LayoutInflater.from(context).inflate(R.layout.layout_complete_cell, null, false);
//                ImageView checkIcon = (ImageView)completeCellView.findViewById(R.id.check_icon);
//                TextView checkDesc = (TextView)completeCellView.findViewById(R.id.checkbox_name);
//                if(resultlistBean.isIsselected()){
//                    checkIcon.setImageResource(R.mipmap.ic_checkbox_checked);
//                    if(resultlistBean.getTypename().equals("+")) {
//                        checkDesc.setText("独立完成");
//
//                    }else if(resultlistBean.getTypename().equals("p+")) {
//                        checkDesc.setText("辅助完成");
//                    }else if(resultlistBean.getTypename().equals("p-")) {
//                        checkDesc.setText("不能完成");
//                    }else {
//                        checkDesc.setText("未完成");
//                    }
//                }else{
//                    checkIcon.setImageResource(R.mipmap.ic_checkbox_unchecked);
//                    checkDesc.setText("");
//                }
//                checkDesc.setTextColor(context.getResources().getColor(R.color.color_base));
//                checkIcon.setVisibility(GONE);
////                checkDesc.setText(resultlistBean.getTypename());
//                completeCellView.setTag(resultlistBean.getId());
//                LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//                layoutParams.weight = 1f;
//                mCompleteCheckboxLayout.addView(completeCellView, layoutParams);
//            }
        }else{
            mContentLayout.setVisibility(View.GONE);
            mDrawerImg.setImageResource(R.mipmap.ic_table_down_srrow);
        }
    }



}